<style lang="less" scoped>
    @import '../../../styles/common.less';
    @import '../../../styles/loading.less';
    @import 'audit.less';
</style>

<template>
    <div>
        <Row style="font-size: 15px;">
            单位名称：<Input v-model="query.orgName" clearable style="width: 15%"></Input>
              联系人：<Input v-model="query.orgContacts" clearable style="width: 15%"></Input>
                状态：<Select v-model="query.type" style="width:8%" filterable clearable>
                            <Option value="0">待审核</Option>
                            <Option value="1">已审核</Option>
                        </Select>
            <span @click="loadList"><Button type="primary" icon="android-search">查询</Button></span>
            <span @click="refresh"><Button type="primary" icon="refresh">清空</Button></span>
        </Row>
        <div class="main-table">
            <Table border :columns="columns" :data="data"></Table>
        </div>
        <div class="pagination">
            <Page :total="total" size="small" :page-size="pageSize" :current.sync="pageIndex" @on-change="loadList" show-total show-elevator></Page>
        </div>
        <Modal
                v-model="viewModalVisible"
                title="机构信息"
                @on-ok="viewModalVisible = false"
                @on-cancel="viewModalVisible = false"
                width="30%">
            <Form :model="formData" :label-width="70">
                <FormItem label="单位名称">
                    <div class="conts">
                        {{formData.orgName}}
                    </div>
                </FormItem>
                <FormItem label="联系人">
                    <div class="conts">
                        {{formData.orgContacts}}
                    </div>
                </FormItem>
                <FormItem label="联系电话">
                    <div class="conts">
                        {{formData.orgPhone}}
                    </div>
                </FormItem>
                <FormItem label="单位邮箱">
                    <div class="conts">
                        {{formData.orgEmail}}
                    </div>
                </FormItem>
                <FormItem label="地址">
                    <div class="conts">
                        {{formData.orgAddress}}
                    </div>
                </FormItem>
            </Form>
        </Modal>
        <Modal v-model="contModalVisible" title="合同管理" width="50%"  @on-cancel="contCancel">
            <Form :model="formData" :label-width="70">
                <Row v-for="(item,index) in contList">
                    <Col span="10">
                    <FormItem label="金额">
                        <InputNumber v-model="item.money" :min="0" style="width:100%"></InputNumber>
                    </FormItem>
                    </Col>
                    <Col span="10">
                        <FormItem label="到期时间">
                            <DatePicker type="date" v-model="item.duedate" @on-change="item.duedate=$event"></DatePicker>
                        </FormItem>
                    </Col>
                    <Col span="4" v-if="index+1==(contList.length)">
                    <Button type="primary" shape="circle" icon="plus" @click="addCont"></Button>
                    <Button type="primary" shape="circle" icon="minus" @click="delCont"></Button>
                    </Col>
                </Row>
            </Form>
            <div slot="footer">
                <Button type="primary" @click="contSave">保存</Button>
                <Button type="ghost" style="margin-left: 8px" @click="contCancel">取消</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
    import util from '@/libs/util';
    import Cookies from 'js-cookie';

    export default {
        name: 'exportable-table',
        data () {
            return {
                columns: [{
                    title: '单位名称',
                    key: 'orgName'
                }, {
                    title: '联系人',
                    key: 'orgContacts'
                }, {
                    title: '联系电话',
                    key: 'orgPhone'
                }, {
                    title: '状态',
                    key: 'type',
                    render:(h,params) => {
                        let texts = '';
                        if(params.row.type == '0'){
                            texts = "待审核"
                        }else if(params.row.type == '1'){
                            texts = "已通过"
                        }
                        return h('div', {
                            props: {
                            },
                        },texts)
                    }
                }, {
                    title: '操作',
                    key: 'action',
                    width: 300,
                    align: 'center',
                    render: (h, params) => {
                        let auditBoo = 'none';
                        let contBoo = 'inline';
                        if(params.row.type == 0 ){
                            auditBoo = 'inline';
                            contBoo = 'none';
                        }
                        return h('div', [
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px'
                                },
                                on: {
                                    click: () => {
                                        this.detail(params.row.id);
                                    }
                                }
                            }, '详情'),
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px',
                                    display:auditBoo
                                },
                                on: {
                                    click: () => {
                                        this.audit(params.row);
                                    }
                                }
                            }, '审核'),
                            h('Button', {
                                props: {
                                    type: 'primary',
                                    size: 'small'
                                },
                                style: {
                                    marginRight: '5px',
                                    display:contBoo
                                },
                                on: {
                                    click: () => {
                                        this.setCont(params.row.id);
                                    }
                                }
                            }, '合同')
                        ]);
                    }
                }],
                data: [],
                formData: {},
                query:{
                    orgName:'',
                    orgContacts:'',
                    type:''
                },
                viewModalVisible: false,
                contModalVisible: false,

                total: 0,
                pageIndex: 1,
                pageSize: 10,

                contList:[],
                orgId:''
            };
        },
        mounted() {
            this.loadList();
        },
        methods: {
            loadList() {
                util.ajax.post('/busine/organize/list', util.toFormData({
                    page: this.pageIndex,
                    size: this.pageSize,
                    orgName: this.query.orgName,
                    orgContacts: this.query.orgContacts,
                    type: this.query.type
                })).then(res => {
                    this.data = res.data.data.list;
                    this.total = res.data.data.total;
                });
            },
            detail(id) {
                util.ajax.post( '/busine/organize/detail', util.toFormData({
                    id: id
                })).then(res => {
                    if (res.data.data) {
                        this.formData = res.data.data;
                        this.viewModalVisible = true;
                    } else {
                        this.$Modal.error({
                            title: '警告',
                            content: '出错了'
                        });
                    }
                });
            },
            audit(obj) {
                let formData = {};
                for (let key in obj) {
                    formData[key] = obj[key];
                }
                formData["createTime"] = this.getDate(formData["createTime"]);
                formData["userName"] = eval('(' + Cookies.get('baseUser') + ')').name;
                util.ajax.post('/busine/organize/audit', util.toFormData(formData)).then(res => {
                    if (res.data.message === 'SUCCESS') {
                        this.$Message.success('保存成功！');
                        this.loadList();
                    } else {
                        this.$Message.warning(res.data.message || '未知错误！');
                    }
                });
            },
            refresh(){
                this.query = {
                    orgName:'',
                    orgContacts:'',
                    type:''
                };
                this.loadList();
            },
            setCont(id){
                this.contModalVisible = true;
                this.orgId = id;
                util.ajax.post('/busine/contract/listByAudit', util.toFormData({
                    id: id
                })).then(res => {
                    if(res.data.data){
                        this.contList = res.data.data;

                        if(this.contList.length == 0){
                            this.contList.splice(0, 0, {
                                money:0,
                                duedate:''
                            });
                        }
                        for(let i=0 ; i<this.contList.length ; i++){
                            this.contList[i].duedate = this.getDate(this.contList[i].duedate)
                        }
                    }
                })
            },
            addCont(){
                const cont = {
                    money:0,
                    duedate:''
                };
                const contList = this.contList;
                this.contList.splice(contList.length, 0, cont);
            },
            delCont(){
                const contList = this.contList;
                if(contList.length>0){
                    this.contList.splice(contList.length-1, 1);
                }
                if(contList.length==0){
                    this.contList.splice(0, 0, {
                        money:0,
                        duedate:''
                    });
                }
            },
            contSave(){
                let _this = this;
                let contList = [];
                for(let i=0 ; i<this.contList.length ; i++){
                    if(this.contList[i].money != ""){
                        this.contList[i].duedate = this.getDate(this.contList[i].duedate)
                        contList.splice(contList.length-1,0,this.contList[i]);
                    }
                }
                util.ajax.post('/busine/contract/saveByAudit', util.toFormData({
                    id:_this.orgId,
                    listStr:JSON.stringify(contList)
                })).then(res => {
                    if (res.data.message === 'SUCCESS') {
                        this.contModalVisible = false;
                        this.$Message.success('保存成功！');
                    } else {
                        this.$Message.warning(res.data.message || '未知错误！');
                    }
                });
            },
            contCancel(){
                this.contModalVisible = false;
            },
            getDate(data) {
                let myDate;

                if (data instanceof Date){
                    myDate = data;
                } else if (typeof(data) == 'number') {
                    myDate = new Date(data);
                } else {
                    return data;
                }

                let Y = myDate.getFullYear(),
                    M = myDate.getMonth() + 1,
                    D = myDate.getDate(),
                    H = myDate.getHours(),
                    Min = myDate.getMinutes(),
                    S = myDate.getSeconds();

                if (M < 10) {
                    M = '0' + M;
                }
                if (D < 10) {
                    D = '0' + D;
                }
                if (H < 10) {
                    H = '0' + H;
                }
                if (Min < 10) {
                    Min = '0' + Min;
                }
                if (S < 10) {
                    S = '0' + S;
                }
                return Y + '-' + M + '-' + D +' '+ H + ':' +Min + ':' + S;
            }
        }
    };
</script>
